﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Sita.Data.Context;
using Sita.Entities;
using Sita.Data.Core;

namespace Sita.Data.Repositories
{
    public class CustomerRepository : GenericRepository<Customer>
    {
        public CustomerRepository() :
            base(new SitaContext())
        { }

        public IList<Customer> GetPaginatedByBusinessUnitID(int businessUnitID, int pageIndex, int pageSize, string sortColumn, bool ascending, out int totalRows)
        {
            RepositoryHelper<Customer> Helper = new RepositoryHelper<Customer>();
            var OrderByExpr = Helper.GetOrderExpression(sortColumn);

            IQueryable<Customer> ReturnSet;

            ReturnSet = (base.StoreContext as ISitaContext).Customers
                .Where(r => r.BusinessUnitID == businessUnitID)
                .Select(buu => buu);

            totalRows = ReturnSet.Count();

            return (ascending)
                                ?
                                    ((IOrderedQueryable<Customer>)System.Linq.Queryable.OrderBy(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Take(pageSize)
                                        .ToList()
                                :
                                    ((IOrderedQueryable<Customer>)System.Linq.Queryable.OrderByDescending(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Take(pageSize)
                                        .ToList();

        }

        public IList<Customer> GetPaginatedByFilter(int businessUnitID, int pageIndex, int pageSize, string sortColumn, bool ascending, string firstName, string lastName, int dni, string phone, string email, out int totalRows)
        {
            RepositoryHelper<Customer> Helper = new RepositoryHelper<Customer>();
            var OrderByExpr = Helper.GetOrderExpression(sortColumn);

            IQueryable<Customer> ReturnSet;

            ReturnSet = (base.StoreContext as ISitaContext).Customers
                .Where(r => r.BusinessUnitID == businessUnitID &&
                        (r.FirstName.Contains(firstName) || string.IsNullOrEmpty(firstName)) &&
                        (r.LastName.Contains(lastName) || string.IsNullOrEmpty(lastName)) &&
                        (dni == 0 || r.DNI == dni) &&
                        (r.Phone.Contains(phone) || r.Phone2.Contains(phone) || string.IsNullOrEmpty(phone)) &&
                        (r.Email.Contains(email) || r.Email2.Contains(email) || string.IsNullOrEmpty(email)))
                .Select(buu => buu);

            totalRows = ReturnSet.Count();

            return (ascending)
                                ?
                                    ((IOrderedQueryable<Customer>)System.Linq.Queryable.OrderBy(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Take(pageSize)
                                        .ToList()
                                :
                                    ((IOrderedQueryable<Customer>)System.Linq.Queryable.OrderByDescending(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Take(pageSize)
                                        .ToList();

        }

        public IList<Customer> GetByBusinessUnitIDByCompanyName(int businessUnitID, string companyName, string sortColumn)
        {
            if (string.IsNullOrEmpty(sortColumn))
                sortColumn = "FirstName";
            if (string.IsNullOrEmpty(companyName))
                companyName = "";

            RepositoryHelper<Customer> Helper = new RepositoryHelper<Customer>();
            var OrderByExpr = Helper.GetOrderExpression(sortColumn);

            IQueryable<Customer> ReturnSet;

            ReturnSet = (base.StoreContext as ISitaContext).Customers
                .Where(c => c.BusinessUnitID == businessUnitID && c.CompanyName.Contains(companyName))
                .Select(c => c);


            return ((IOrderedQueryable<Customer>)System.Linq.Queryable.OrderBy(ReturnSet, OrderByExpr))
                                        .ToList();
        }

        public Customer GetByIDWithVehicles(int CustomerID)
        {
            Customer ReturnSet;
            ReturnSet = (base.StoreContext as ISitaContext).Customers
                .Where(r => r.CustomerID == CustomerID)
                .Include("Vehicles")
                .SingleOrDefault();

            return ReturnSet;
        }
    }
}
